import brandList from '@/components/filter-list/brand.vue'
import carList from './components/carList.vue'
export default {
  components: {
    brandList,
    carList
  },
  data() {
    return {
      pageLoading: false,
      isHeight: 0,
      navHight: 0,
      carNmber: 0,
      //下拉标题
      sort: '智能排序',
      paymentTitlie: '首付',
      mounthlyTitle: '月供',

      carlist: [],
      // 面包屑
      history: {
        type: '',
        zhineng: '',
        brand: '',
        payment: '',
        mounthly: '',
      },
      searchKey: '', //搜索历史关键字
      type: -1,
      iconSearch: '',
      /*一成首付*/
      paymentValue: 0,//智能排序
      isOpen: false,
      //1成智能排序（通用）
      paymentsSort: [{
        id: 0,
        name: '智能排序'
      },
      {
        id: 1,
        name: '首付最低'
      },
      {
        id: 2,
        name: '月供最低'
      },
      ],
      //首付排序
      paymentsPriceType: [{
        id: 0,
        name: '价格不限',
        max: null,
        min: null
      },
      {
        id: 1,
        name: '0首付',
        max: 0,
        min: 0
      },
      {
        id: 2,
        name: '5千以内',
        max: 4999,
        min: 0
      },
      {
        id: 3,
        name: '5千-1万',
        max: 10000,
        min: 5000
      },
      {
        id: 4,
        name: '1-2万',
        max: 20000,
        min: 10000
      },
      {
        id: 5,
        name: '2-3万',
        max: 30000,
        min: 20000
      },
      {
        id: 6,
        name: '3-4万',
        max: 40000,
        min: 30000
      },
      {
        id: 7,
        name: '4-5万',
        max: 50000,
        min: 40000
      },
      {
        id: 8,
        name: '5-6万',
        max: 60000,
        min: 50000
      },
      {
        id: 9,
        name: '6万以上',
        max: 999999999,
        min: 60000
      }
      ],
      paymentsPriceValue: -1,
      paymentsPrice: [{
        id: 1,
        name: '0首付',
        max: 0,
        min: 0
      },
      {
        id: 2,
        name: '5千以内',
        max: 4999,
        min: 0
      },
      {
        id: 3,
        name: '5千-1万',
        max: 10000,
        min: 5000
      },
      {
        id: 4,
        name: '1-2万',
        max: 20000,
        min: 10000
      },
      {
        id: 5,
        name: '2-3万',
        max: 30000,
        min: 20000
      },
      {
        id: 6,
        name: '3-4万',
        max: 40000,
        min: 30000
      },
      {
        id: 7,
        name: '4-5万',
        max: 50000,
        min: 40000
      },
      {
        id: 8,
        name: '5-6万',
        max: 60000,
        min: 50000
      },
      {
        id: 9,
        name: '6万以上',
        max: 999999999,
        min: 60000
      }
      ],
      //月供排序
      mounthlyPrice: [{
        id: 0,
        name: '价格不限',
        min: 0,
        max: 0
      },
      {
        id: 1,
        name: '1千以内',
        min: 0,
        max: 999
      },
      {
        id: 2,
        name: '1-2千',
        min: 1000,
        max: 2000
      },
      {
        id: 3,
        name: '2-3千',
        min: 2000,
        max: 3000
      },
      {
        id: 4,
        name: '3-4千',
        min: 3000,
        max: 4000
      },
      {
        id: 5,
        name: '4-5千',
        min: 4000,
        max: 5000
      },
      {
        id: 6,
        name: '5-6千',
        min: 5000,
        max: 6000
      },
      {
        id: 7,
        name: '6千以上',
        min: 6000,
        max: 999999999
      },
      ],
      mounthlyPriceValue: 0,
      carIndex: 1,//当前页
      paymentsMin: 0,//首付最低
      paymentsMax: 0,//首付筛选最高
      mounthlyMin: 0,//月供最低
      mounthlyMax: 0,//月供最高
      brandId: '',//品牌id
      loadStatus: 'nomore',//下拉刷新状态
    }
  },
  computed: {
    stickyHeight() {
      return Math.floor(Number(this.navHight) / (uni.upx2px(Number(this.navHight)) / Number(this.navHight))) - 1
    }
  },
  created(){
    this.buyHome()
  },
  onReachBottom(){
	  this.buyHome()
  },

  methods: {
    clickLoadMore(){
      this.buyHome()
    },
    onPullDownRefresh (){
	  // this.buyList()
      this.buyHome()

    },
    // 加载前、中、后
    moreStatus(status) {
		  this.loadStatus = status
    },
    async buyHome(){
      uni.showLoading({
			    title: '加载中'
      })
      this.loadStatus = 'loading'
      try{
        let data = {
          'pageIndex': this.carIndex,
          'pageSize': 10,
          'search': this.searchKey,
          'brandId': String(this.brandId), //品牌id
          'sorting': this.paymentValue,
          'lowPricetyoe': this.paymentsMin, //一成首付最低价：默认传null
          'highPricetyoe': this.paymentsMax, //一成首付最高价：默认传null
          'lowMonthlyPayments': this.mounthlyMin||null, //月供最低价：默认传null
          'highMonthlyPayments': this.mounthlyMax||null //月供最低价：默认传null
        }
        const res = await this.$api.buyCar.goodsCarSourceLowList(data)

        this.carNmber = res.data.total
        this.carlist = [...this.carlist, ...res.data.list]
        uni.stopPullDownRefresh()
        this.$nextTick(() => {
          this.pageLoading = true
        })
        // this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        uni.hideLoading()
        if(res.data.total <= 10){
				  this.loadStatus = 'nomore'
        }else{
				  this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        if (res.data.list.length === 0) return false
        this.carIndex += 1 // 当前页加1
      }catch(e){
        uni.stopPullDownRefresh()
        uni.hideLoading()
      }
    },

    // 通用品牌排序
    brandListChange(id,type) {
		  this.brandId = id
      this.carlist = []
      this.carIndex = 1
		  this.history.brand = type.brand
      this.buyHome()
      this.$refs.paymentSort.close()
    },
    //重置
    reset(){
      this.sort = '智能排序',
      this.paymentTitlie = '首付',
      this.mounthlyTitle = '月供',
      this.paymentValue = 0
      this.paymentsPriceValue = -1
      this.mounthlyPriceValue = -1
      this.history = {
			  type: '',
			  zhineng: '',
			  brand: '',
			  payment: '',
			  mounthly: '',
      }
      this.carIndex = 1
      this.carlist = []
      this.brandId = ''
      this.mounthlyMin = 0
      this.mounthlyMax = 0
      this.paymentsMin = null
      this.paymentsMax = null
      this.buyHome()
    },
    delZhineng() {
      this.carIndex = 1
      this.carlist = []
      this.history.zhineng = ''
      this.paymentValue = 0
      this.buyHome()
    },
    delBrand() {
      this.carIndex = 1
      this.carlist = []
      this.history.brand = ''
      this.brandId = ''
      this.buyHome()
    },
    //首付
    delPayment() {
      this.carIndex = 1
      this.carlist = []
      this.paymentsPriceValue = -1
      this.history.payment = ''
      this.paymentsMin = null,      this.paymentsMax = null,
      this.buyHome()
    },
    //月供
    delMounthly() {
      this.carIndex = 1
      this.carlist = []
      this.mounthlyMin = 0,
      this.mounthlyPriceValue = -1
      this.history.mounthly = ''
      this.mounthlyMax = 0,
      this.buyHome()
    },
    //搜索
    onSearch(e) {

      this.carlist = []
      this.carIndex = 1
      if (e === '') {
        uni.showToast({
          title: '请输入关键字',
          icon: 'none',
          duration: 1000
        })
        return false
      }
      this.searchKey = e
      this.buyHome()
    },
    change(e){
      if(e === ''){
        this.carlist = []
        this.carIndex = 1
        this.buyHome()
      }
    },
    //下拉关闭
    close(e) {
      this.isOpen = false

    },
    //下拉列表打开
    open(e) {
      this.isOpen = true

    },
    //筛选
    paymentsType(item) {
      this.type = item.id
      this.carIndex = 1
      this.carlist = []
      this.paymentsMin = item.min
      this.paymentsMax = item.max
      this.history.type = item.name
      this.$u.debounce(this.buyHome, 500)
      // this.buyHome()

    },
    closePayment() {
      this.$refs.paymentSort.close()
    },
    // 智能排序
    paymentSort(item) {

      this.paymentValue = item.id
      this.carIndex = 1
      this.carlist = []
      this.sort = item.name
      this.history.zhineng = item.name
      this.$refs.paymentSort.close()
      this.buyHome()
    },
    //首付
    paymentsSrot(item) {

      this.carIndex = 1
      this.carlist = []
      this.paymentTitlie = item.name
      this.paymentsPriceValue = item.id
      this.paymentsMin = item.min
      this.paymentsMax = item.max
      this.history.payment = item.name
      this.$refs.paymentSort.close()
      this.buyHome()
    },
    //月供
    mounthlySort(item) {
      this.mounthlyTitle = item.name
      this.mounthlyPriceValue = item.id
      this.carIndex = 1
      this.carlist = []
      this.history.mounthly = item.name
      this.mounthlyMin = item.min
      this.mounthlyMax = item.max

      this.$refs.paymentSort.close()
      this.buyHome()
    },
  }
}
